package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cm.domain.ZMCMDepot;

/**材料使用管理--库房信息
 * <p>Title: ServiceZMCM10.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-5-26 下午04:49:43  余虎  Create
 *</pre>
 */
public class ServiceZMCM10 extends ServiceEPBase {
	
	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM05</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-26下午04:50:57 余虎
	 * </p>
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM05</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-26下午04:50:57 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		if(inInfo.getBlock(EiConstant.queryBlock) == null)
		{
			EiBlock block = new EiBlock(EiConstant.queryBlock);
			inInfo.addBlock(block);
		}

		inInfo.getBlock(EiConstant.queryBlock).setCell(0, "fdDepotLevel", "二级");
		EiInfo outInfo = super.query(inInfo, "ZMCMDepot.query",new ZMCMDepot());
		return outInfo;
	}

	/**新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM05</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-26下午04:50:57 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		
		boolean validate = this.validateForDepotLevel(inInfo, "ZMCMDepot.queryForValidate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			boolean validateValue = validate(inInfo, "ZMCMDepot.validate", new HashMap<String, String>());
			
			if(validateValue) {
				return inInfo;
			}
			else {
				
				inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_DEPOT "));//主键
				inInfo.getBlock("result").setCell(0, "fdStatus", "在用");
				EiInfo outInfo = super.insert(inInfo, "ZMCMDepot.insert");
				return outInfo;
			}
		}
	}

	/**库房信息修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM05</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-26下午04:50:57 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		boolean validateValue = validate(inInfo, "ZMCMDepot.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		EiInfo outInfo = super.update(inInfo, "ZMCMDepot.updateWithoutStatus");
		return outInfo;
	}
	/**
	 * 启用
	 * <p>Function: modifyPlan</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-27下午03:25:43 余虎
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo) {
		  EiInfo _temp = super.update(inInfo, "ZMCMDepot.modifyStart");
		  EiInfo  outInfo = this.query(inInfo);
	      outInfo.setMsg(_temp.getMsg());
	      outInfo.setDetailMsg(_temp.getDetailMsg());
	      return outInfo;
	        
	}
	/**
	 * 停用
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-27下午03:26:46 余虎
	 * </p>
	 *
	 */
		public EiInfo modifyStop(EiInfo inInfo) {
		  EiInfo _temp = super.update(inInfo, "ZMCMDepot.modifyStop");
		  EiInfo  outInfo = this.query(inInfo);
		  outInfo.setMsg(_temp.getMsg());
		  outInfo.setDetailMsg(_temp.getDetailMsg());
		  return outInfo;
		}

	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM05</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-26下午04:50:57 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		// TODO Auto-generated method stub
		return super.delete(inInfo);
	}
	/**
	 * 编号名称重复校验
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1下午07:58:53 余虎
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdDepotCode = inInfo.getBlock("result").getCellStr(0, "fdDepotCode");
		String fdDepotName = inInfo.getBlock("result").getCellStr(0, "fdDepotName");
		params.put("fdDepotCode", fdDepotCode);
		params.put("fdDepotName", fdDepotName);
		List result = dao.query(sqlName, params);
		
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCMDepot _temp = (ZMCMDepot)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdDepotCode() != null && _temp.getFdDepotCode().equals(fdDepotCode)) {
				msg.append(" 库房编号重复 ！ ");
			}
			if(_temp.getFdDepotName() != null && _temp.getFdDepotName().equals(fdDepotName)) {
				msg.append(" 库房名称重复 ！ ");
			}
			msg.append("");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 * 校验一级库房
	 * <p>Function: validateForDepotLevel</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-20下午01:21:34 余虎
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validateForDepotLevel(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdDepotLevel = inInfo.getBlock("result").getCellStr(0, "fdDepotLevel");
		if(fdDepotLevel != "一级" && !fdDepotLevel.equals("一级"))//如果提交的不是一级库则不进行校验
		{
			return false;
		}
	    params.put("fdDepotLevel", fdDepotLevel);//如果提交的是一级库则传到后台
		List result = dao.query(sqlName, params);
		
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("提交失败，仅允许库房建立一个一级库！");
			return true;
		}
		return false;
	}

}
